import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false
      }
    ]
  },
  getters: {
    totalPrice (state) {
      return state.goodList.reduce((sum, item) => sum + (item.checked ? item.price * item.num : 0), 0)
    }
  },
  // 修改state里面的内容 添加，删除，减少，选框
  mutations: {
    subChange (state, payload) {
      console.log(payload)
      state.goodList[payload.index].num = payload.num
    },
    // paayod就是接受的传过来的索引
    delFn (state, payload) {
      state.goodList.splice(payload, 1)
    },
    checkFn (state, payload) {
      // 接收多个参数的话就是对象，不是的payload只是一个参数
      state.goodList[payload].checked = !state.goodList[payload].checked
    },
    checkedchange (state, val) {
      console.log(val)
      state.goodList.forEach(item => { item.checked = val })
    }
  },
  actions: {
  },
  modules: {
  }
})
export default store
